﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace SampleApplication
{
    public class Utility
    {
        /// <summary>
        /// Converts an enum into a list of Value, Name pairs.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IEnumerable CollectionFromEnum(Type type, bool splitOnCase = false)
        {
            if (type == null) { throw new ArgumentNullException("type"); }
            if (!type.IsEnum) { throw new ArgumentException("type must be an enum", "type"); }

            if (splitOnCase)
            {
                return System.Enum.GetValues(type).Cast<object>().Select(
                    tr => new
                    {
                        Name = Regex.Replace(tr.ToString(), "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "),
                        Value = tr
                    });
            }
            else
            {
                return System.Enum.GetValues(type).Cast<object>().Select(
                    tr => new { Name = tr.ToString(), Value = tr });
            }
        }



        /// <summary>
        /// The .Net Random class gives you statistically random, repeatable values
        /// which aren't really random. We're using the MS example for true random
        /// numbers for fun. 
        /// </summary>
        public static int GetNext(int min, int max)
        {
            var rngCryptoServiceProvider = new RNGCryptoServiceProvider();
            var randomBytes = new byte[4];
            rngCryptoServiceProvider.GetBytes(randomBytes);
            var result = Math.Abs(BitConverter.ToInt32(randomBytes, 0));
            return result % max + min;
        } 
    }
}
